package jee.boot.module.bpm.rest;

import jee.boot.common.basic.Page;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.entity.bpm.BusinessData;
import jee.boot.entity.bpm.FormDefinition;
import jee.boot.module.bpm.service.BusinessDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
 * 业务表数据
 */
@RestController
@RequestMapping("/businessData/{definitionName}")
public class BusinessDataRestController {
    @Autowired
    private BusinessDataService service;


    /**
     * 查看
     *
     * @param data
     * @return
     */
    @PostMapping("/get")
    public ResponseMessage<Map<String, Object>> get(@PathVariable("definitionName") String definitionName, @RequestBody Map<String, Object> data) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        entity.setData(data);
        entity = service.get(entity);
        return Result.success(entity.getData());
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/list")
    public ResponseMessage<Page<Map<String, Object>>> list(@PathVariable("definitionName") String definitionName, @RequestBody BusinessData entity) {
        entity.setDefinition(new FormDefinition(definitionName));
        if (entity.getPage() == null) {
            entity.setPage(new Page<>());
        }
        Page<Map<String, Object>> findPage = service.findPage(entity);
        return Result.success(findPage);
    }

    /**
     * 新增
     *
     * @param data
     * @return
     */
    @PostMapping("/save")
    public ResponseMessage<Map<String, Object>> save(@PathVariable("definitionName") String definitionName, @RequestBody Map<String, Object> data) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        entity.setData(data);
        service.save(entity);
        return Result.success(entity.getData());
    }


    /**
     * 修改
     *
     * @param data
     * @return
     */
    @PostMapping("/update")
    public ResponseMessage<Map<String, Object>> update(@PathVariable("definitionName") String definitionName, @RequestBody Map<String, Object> data) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        entity.setData(data);
        service.update(entity);
        return Result.success(entity.getData());
    }


    /**
     * 删除
     *
     * @param data
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage<BusinessData> delete(@PathVariable("definitionName") String definitionName, @RequestBody Map<String, Object> data) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        entity.setData(data);
        service.delete(entity);
        return Result.success();
    }
    /**
     * 导出
     *
     * @param entity
     * @return
     */
    @PostMapping("/export")
    public ResponseMessage<BusinessData> export(@PathVariable("definitionName") String definitionName, @RequestBody BusinessData entity) {
        entity.setDefinition(new FormDefinition(definitionName));
        if (entity.getPage() == null) {
            entity.setPage(new Page<>());
        }
        service.exportData(entity);
        return Result.success();
    }
    /**
     * 导入模板
     *
     * @return
     */
    @PostMapping("/template")
    public void importTemplate(@PathVariable("definitionName") String definitionName,HttpServletResponse response) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        service.importTemplate(entity, response);
    }

    /**
     * 导入模板
     *
     * @return
     */
    @GetMapping("/template")
    public  void importTemplateGet(@PathVariable("definitionName") String definitionName, HttpServletResponse response) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        service.importTemplate(entity, response);
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public ResponseMessage<Object> importData(@PathVariable("definitionName") String definitionName,MultipartFile file) {
        BusinessData entity=new BusinessData();
        entity.setDefinition(new FormDefinition(definitionName));
        Object data = service.importData(entity,file);
        return Result.success(data);
    }
}
